/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.living.filedownloader.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.R;
import com.living.filedownloader.DownLoadManger;
import com.living.filedownloader.bean.TasksManagerModel;
import com.living.filedownloader.db.DownLoadDBManger;
import com.living.filedownloader.event.EventDownloadComplete;
import com.living.filedownloader.ui.adapter.TaskItemDownloadedAdapter;
import com.living.filedownloader.ui.batch.DownloadBatchActivity;
import com.living.player.PlayUtils;
import com.living.widget.BottomMenuView;
import com.living.widget.MorePopupWindow;
import com.md.basedpc.PHNoDoubleClickListener;
import com.md.livingwidget.CoreBaseActivity;
import com.md.livingwidget.emptyview.PHEmptyViewUI;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地音频--展示已经下载的数据集合
 * Created by wangq
 */
public class TasksDownloadedActivity extends CoreBaseActivity implements OnItemClickListener, OnItemChildClickListener {

    private View layout_downloading;
    private TextView tv_downloading_count;

    private View layout_downloaded;
    private TextView iv_downloaded_play;
    private ImageView iv_downloaded_edite;

    private TaskItemDownloadedAdapter taskItemAdapter;
    private ArrayList<TasksManagerModel> downloadedList = new ArrayList<>();
    private MorePopupWindow morePopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloaded_tasks);
        setToolbarTitle("本地音频");
        morePopupWindow = new MorePopupWindow(this, new MorePopupWindow.OnMoreItemClick() {
            @Override
            public void onClickAdd(Object object) {
                TasksManagerModel tasksManagerModel = (TasksManagerModel) object;
            }

            @Override
            public void onClickDownload(Object object) {
                // todo nothing
            }

            @Override
            public void onClickShare(Object object) {
                TasksManagerModel tasksManagerModel = (TasksManagerModel) object;
            }

            @Override
            public void onClickDelete(Object object) {
                TasksManagerModel tasksManagerModel = (TasksManagerModel) object;
                taskItemAdapter.remove(tasksManagerModel);
                DownLoadManger.getInstance().deleteAudio(tasksManagerModel.getAlbumId(), tasksManagerModel.getAudioId(), tasksManagerModel.getUrl());
            }
        });
        morePopupWindow.setMenuShows(true, false, true, true);

        layout_downloading = findViewById(R.id.layout_downloading);
        tv_downloading_count = findViewById(R.id.tv_downloading_count);

        layout_downloaded = findViewById(R.id.layout_downloaded);
        iv_downloaded_play = findViewById(R.id.iv_downloaded_play);
        iv_downloaded_edite = findViewById(R.id.iv_downloaded_edite);

        layout_downloading.setOnClickListener(this);
        iv_downloaded_edite.setOnClickListener(this);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        taskItemAdapter = new TaskItemDownloadedAdapter();
        recyclerView.setAdapter(taskItemAdapter);

        PHEmptyViewUI phEmptyViewUI = new PHEmptyViewUI(this);
        phEmptyViewUI.setEmptyText("没有已下载歌曲");
        taskItemAdapter.setEmptyView(phEmptyViewUI);

        taskItemAdapter.setOnItemClickListener(this);

        taskItemAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.layout_downloading) {
            // 进入[正在下载] 页面
            startActivity(new Intent(this, TasksDownloadingActivity.class));
        } else if (v.getId() == R.id.iv_downloaded_edite) {
            // 进入【批量删除】页面
            Intent intent = new Intent(this, DownloadBatchActivity.class);
            intent.putExtra("list", downloadedList);
            startActivity(intent);
        }
    }

    @Override
    public void onEventBase(Object event) {
        super.onEventBase(event);
        if (event instanceof EventDownloadComplete) {
            refreshData();
        }
    }

    /**
     * 刷新下载列表
     */
    private void refreshData() {
        // 正在下载
        List<TasksManagerModel> downloadingList = DownLoadManger.getInstance().getDownloadingList();
        if (downloadingList.size() > 0) {
            layout_downloading.setVisibility(View.VISIBLE);
            tv_downloading_count.setText("正在下载" + downloadingList.size() + "首歌曲");
        } else {
            layout_downloading.setVisibility(View.GONE);
        }
        // 已下载数据
        downloadedList = (ArrayList<TasksManagerModel>) DownLoadManger.getInstance().getDownloadedList();
        if (downloadedList.size() > 0) {
            layout_downloaded.setVisibility(View.VISIBLE);
        } else {
            layout_downloaded.setVisibility(View.GONE);
        }
        iv_downloaded_play.setText("播放全部(" + downloadedList.size() + ")");
        taskItemAdapter.setNewInstance(downloadedList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        List<Audio> audioList = DownLoadDBManger.getInstance().getDownloadedAudios();
        Audio audio = DownLoadDBManger.getInstance().getDownloadedAudio(taskItemAdapter.getItem(position).getAudioId());
        if (audio != null) {
            PlayUtils.addAudioListPlay(this, audio, audioList);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        morePopupWindow.showDialog(taskItemAdapter.getItem(position));
    }


}
